package main

import (
	"errors"
	"github.com/google/uuid"
)

type UserService struct{}

// Login 登录的数据库操作
func (s *UserService) Login(u *SysUser) (*SysUser, error) {
	user := SysUser{}
	result := GlobalDb.Where("username = ?", u.Username).Find(&user)
	if result.RowsAffected == 0 {
		return nil, errors.New("账号不存在")
	}
	if !BcryptCheck(u.Password, user.Password) {
		return nil, errors.New("密码错误")
	}
	return &user, nil
}

// Register 注册的数据库操作
func (s *UserService) Register(u *SysUser) (*SysUser, error) {
	u.UUID = uuid.New()
	u.Password = BcryptHash(u.Password)
	result := GlobalDb.Create(&u)
	if result.RowsAffected == 0 {
		return nil, errors.New("RowsAffected is 0")
	}
	return u, nil
}
